package com.example.play.service.impl;


import com.example.play.entity.Scenic;
import com.example.play.request.ScenicPageReq;
import com.example.play.request.ScenicReq;
import com.example.play.dao.ScenicMapper;
import com.example.play.service.ScenicService;
import cn.hutool.core.bean.BeanUtil;
import com.api.core.exception.ServiceException;
import com.api.core.result.PageView;
import com.api.core.util.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.play.util.LngLatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;

/**
 * 景点(Scenic)表控制层
 *
 * @author makejava
 * @since 2022-02-26 13:44:49
 */
@Service("ScenicService")
public class ScenicServiceImpl extends PageUtils implements ScenicService {

    @Autowired
    private ScenicMapper scenicMapper;

    /**
     * 修改数据
     *
     * @param scenic 实例对象
     * @return 实例对象
     */
    @Override
    public Scenic update(Scenic scenic) {
        scenicMapper.updateById(scenic);
        return scenicMapper.selectById(scenic.getId());
    }

    /**
     * 添加实现
     *
     * @param scenicReq !{tableInfo.comment}参数对象
     * @return Scenic
     */
    @Override
    public Scenic addScenic(ScenicReq scenicReq) {
        Optional.ofNullable(scenicReq).orElseThrow(() -> new ServiceException("请求参数不能为空"));
        Scenic scenic = new Scenic();
        BeanUtil.copyProperties(scenicReq, scenic);
        scenic.setStatus(1);
        scenic.setCreatedTime(LocalDateTime.now());
        scenicMapper.insert(scenic);
        return scenic;
    }

    /**
     * 查询景点 列表
     *
     * @param scenicReq 编号
     * @return Scenic
     */
    @Override
    public PageView findScenicList(ScenicPageReq scenicReq) {
        Optional.ofNullable(scenicReq).orElseThrow(() -> new ServiceException("请求参数不能为空"));
        LambdaQueryWrapper<Scenic> lambdaQuery = getScenicQuery(scenicReq);
        Page<Scenic> page = new Page<>(scenicReq.getNowPage(), scenicReq.getPageSize());
        Page scenicList = scenicMapper.selectPage(page, lambdaQuery);
        return getPageView(scenicList);
    }

    /**
     * 查询景点 详情
     *
     * @param scenicId scenic编号
     * @return Scenic
     */
    @Override
    public Scenic getScenicById(Integer scenicId) {
        return scenicMapper.selectById(scenicId);
    }

    /**
     * 删除景点
     *
     * @param scenicId scenic编号
     * @return Scenic
     */
    @Override
    public boolean deleteScenic(Integer scenicId) {
        Scenic scenic = Optional.ofNullable(scenicMapper.selectById(scenicId))
                .orElseThrow(() -> new ServiceException("scenic不存在！"));
        ;
        return scenicMapper.updateById(scenic) > 0;
    }


    /**
     * 景点 条件构造器
     *
     * @param scenicReq 景点 查询条件
     * @return Wrappers
     */
    public LambdaQueryWrapper<Scenic> getScenicQuery(ScenicPageReq scenicReq) {
        LambdaQueryWrapper<Scenic> lambdaQuery = Wrappers.lambdaQuery();

        Map map = LngLatUtil.getAround(scenicReq.getLat(), scenicReq.getLng(), scenicReq.getDistanceRange());
        if (!"".equals(scenicReq.getScenicName())) {
            lambdaQuery.like(Scenic::getName, scenicReq.getScenicName());
        }
        if (!scenicReq.getLat().equals("")) {
            lambdaQuery.le(Scenic::getLng, map.get("maxLng"));
            lambdaQuery.ge(Scenic::getLng, map.get("minLng"));
            lambdaQuery.le(Scenic::getLat, map.get("maxLat"));
            lambdaQuery.ge(Scenic::getLat, map.get("minLat"));
        }
        lambdaQuery.eq(Scenic::getStatus, 1);
        return lambdaQuery;
    }
}
